<!--
  * 通道表
  *
  * @Author:    zzb
  * @Date:      2025-01-23 04:08:26
-->
<template>
  <!---------- 查询表单form begin ----------->

  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-col :span="4">
        <a-form-item label="通道类型" class="smart-query-form-item">
          <a-select v-model:value="queryForm.channelType" allowClear :options="ZZB_CHANNEL_TYPE" placeholder="请选择通道类型" />
        </a-form-item>
      </a-col>
      <a-col :span="6">
        <a-form-item label="创建日期" class="smart-query-form-item">
          <a-range-picker :allowClear="false" :inputReadOnly="true" v-model:value="queryForm.cTime" @change="onChangeTime" />
        </a-form-item>
      </a-col>
      <a-col :span="4">
        <a-form-item class="smart-query-form-item">
          <a-button type="primary" @click="onSearch">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="resetQuery" class="smart-margin-left10">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-form-item>
      </a-col>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-title">数据列表</div>
      <div class="smart-table-operate-block">
        <!-- <TableOperator v-model="columns" :tableId="null" :refresh="queryData" /> -->
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格 begin ----------->
    <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :loading="tableLoading" :pagination="false">
      <template #bodyCell="{ record, column }">
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button @click="showRelation(record)" type="link">分账关系维护</a-button>
            <a-button @click="showProduct(record)" type="link">产品管理</a-button>
          </div>
        </template>
      </template>
    </a-table>
    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageNo"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>
  <AllocRelationList v-model:open="channelRelation.open" :rowData="channelRelation.rowData" />
  <ChannelProduct v-model:open="channelProduct.open" :rowData="channelProduct.rowData" />
</template>
<script setup>
  import { reactive, ref, onMounted, computed, getCurrentInstance } from 'vue'
  import { message } from 'ant-design-vue'
  import { SmartLoading } from '/@/components/framework/smart-loading'
  import { channelApi } from '/@/api/business/b-merchant/channel/channel-api'
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const'
  import { privilege } from '/@/utils/common'
  import { selectDictLabel } from '/@/utils/common'
  import dayjs from 'dayjs'
  import {
    CHANNEL_BUSINESS_TYPE, // 通道业务类型
    ZZB_CHANNEL_TYPE, // 通道类型
    REGISTER_STATUS, // 进件状态
  } from '/@/constants/dict'
  import ChannelProduct from './channel-product.vue'
  import AllocRelationList from './alloc-relation-list.vue'

  // console.log('useUserStore===>', useUserStore().userInfo)
  const { proxy } = getCurrentInstance()

  const statusTypeColorArray = ['red', 'blue']
  // ---------------------------- 表格列 ----------------------------

  const columns = ref([
    {
      title: '序号',
      dataIndex: 'index',
      width: 60,
      customRender: ({ index }) => {
        return (queryForm.value.pageNo - 1) * queryForm.value.pageSize + index + 1
      },
    },
    {
      title: '通道类型',
      dataIndex: 'channelType',
      customRender: ({ text }) => {
        return selectDictLabel(ZZB_CHANNEL_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '是否直连',
      dataIndex: 'directFlag',
      customRender: ({ text }) => {
        return text ? '是' : '否'
      },
      ellipsis: true,
    },
    {
      title: '是否已开通分账',
      dataIndex: 'allocFlag',
      customRender: ({ text }) => {
        return text ? '是' : '否'
      },
      ellipsis: true,
    },
    {
      title: '签约账号',
      dataIndex: 'account',
      ellipsis: true,
    },
    {
      title: '创建日期',
      dataIndex: 'cTime',
      customRender: (column) => dayjs(column.text).format('YYYY-MM-DD HH:mm:ss'),
      width: 180,
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      minWidth: 180,
    },
  ])

  // ---------------------------- 查询数据表单和方法 ----------------------------

  const queryFormState = {
    cTime: undefined, //[dayjs().startOf('day'), dayjs().endOf('day')],
    cTimeStart: undefined, //dayjs().startOf('day').valueOf(),
    cTimeEnd: undefined, //dayjs().endOf('day').valueOf(),
    channelType: undefined,
    status: undefined,
    pageNo: 1,
    pageSize: 10,
  }
  const onChangeTime = (data) => {
    queryForm.value.cTimeStart = data[0].startOf('days').valueOf()
    queryForm.value.cTimeEnd = data[1].endOf('days').valueOf()
  }
  // 查询表单form
  const queryForm = ref({ ...queryFormState })
  // 表格加载loading
  const tableLoading = ref(false)
  // 表格数据
  const tableData = ref([])
  // 总数
  const total = ref(0)

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.value.pageSize
    Object.assign(queryForm.value, queryFormState)
    queryForm.value.pageSize = pageSize
    queryData()
  }

  // 搜索
  function onSearch() {
    queryForm.value.pageNo = 1
    queryData()
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true
    try {
      let queryResult = await channelApi.merchantsChannelPage(queryForm.value)
      tableData.value = queryResult.data
      total.value = queryResult.total
    } catch (e) {
      console.log(e)
    } finally {
      tableLoading.value = false
    }
  }

  // const getBusinessName = (value) => {
  //   const item = CHANNEL_BUSINESS_TYPE.find((item) => item.value === value)
  //   return item ? item.itemName : null
  // }

  onMounted(() => {
    queryData()
    // getBusinessList()
  })

  // ---------------------------- 关系维护 ----------------------------
  const channelRelation = ref({
    open: false,
    rowData: null,
  })
  function showRelation(data) {
    channelRelation.value = { open: true, rowData: data }
  }
  // ---------------------------- 关系维护 ----------------------------

  /* 产品管理0 */
  const channelProduct = ref({
    open: false,
    rowData: null,
  })
  const showProduct = (row) => {
    channelProduct.value = { open: true, rowData: row }
  }
  /* 产品管理0 */
</script>
